package co.yixiang.modules.user.service.impl;

import cn.hutool.core.util.ObjectUtil;
import co.yixiang.common.service.impl.BaseServiceImpl;
import co.yixiang.common.utils.QueryHelpPlus;
import co.yixiang.domain.PageResult;
import co.yixiang.dozer.service.IGenerator;
import co.yixiang.modules.user.domain.Area;
import co.yixiang.modules.user.service.AreaService;
import co.yixiang.modules.user.service.dto.AreaDto;
import co.yixiang.modules.user.service.dto.AreaQueryCriteria;
import co.yixiang.modules.user.service.mapper.AreaMapper;
import co.yixiang.utils.FileUtil;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;

/**
* @author lsh
* @date 2021-04-19
*/
@Service
@AllArgsConstructor
//@CacheConfig(cacheNames = "area")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class AreaServiceImpl extends BaseServiceImpl<AreaMapper, Area> implements AreaService {

    private final IGenerator generator;
    private final AreaMapper areaMapper;

    @Override
    //@Cacheable
    public PageResult<AreaDto> queryAll(AreaQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<Area> page = new PageInfo<>(queryAll(criteria));
        return generator.convertPageInfo(page,AreaDto.class);
    }

    @Override
    //@Cacheable
    public PageResult<AreaDto> query4operate(AreaQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<Area> page = new PageInfo<>(queryAll4operate(criteria));
        return generator.convertPageInfo(page,AreaDto.class);
    }


    @Override
    //@Cacheable
    public List<Area> queryAll(AreaQueryCriteria criteria){
        return baseMapper.selectList(QueryHelpPlus.getPredicate(Area.class, criteria));
    }

    @Override
    //@Cacheable
    public List<Area> queryAll4operate(AreaQueryCriteria criteria){
        String name ="";
        if(ObjectUtil.isNotEmpty(criteria.getName())){
            name =criteria.getName();
        }
        return areaMapper.query4operate(name);
    }


    @Override
    public void download(List<AreaDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (AreaDto area : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("名称", area.getName());
            map.put("上级区域", area.getParentId());
            map.put("状态", area.getEnabled());
            map.put("创建日期", area.getCreateTime());
            map.put(" updateTime",  area.getUpdateTime());
            map.put(" isDel",  area.getIsDel());
            map.put(" level",  area.getLevel());
            map.put(" sequence",  area.getSequence());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
